<?php

namespace app\bh\controller;


use app\bh\model\DataTypeModel;
use app\bh\model\FileModel;
use app\bh\model\QuyuModel;
use app\bh\model\UserModel;
use think\Controller;
use think\Error;
use think\File;
use think\Request;

class FileController extends BaseAdminController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
//        $files = $this->getAllFile();
        //管理员获取全部的data
        if (self::$user['roles'] == 0) {
            $datas = FileModel::select()->toArray();
        } else {
            //判断区域数据
//
            $quyu = QuyuModel::find(self::$user['qyid']);
            $allquyu = QuyuModel::where('fid', 'like', $quyu->fid . "%")->select()->toArray();
            $users = [];
            foreach ($allquyu as $v) {
                $user = UserModel::where("qyid", $v['id'])->select();
                if (count($user) > 0) {
                    foreach ($user as $v) {
                        array_push($users, $v);
                    }
                }
            }
            $datas = [];
            foreach ($users as $v) {
                $data = FileModel::where('uid', $v['id'])->select()->toArray();

                if (count($data) > 0) {
                    foreach ($data as $v) {
                        array_push($datas, $v);
                    }
                }
            }
//            dump($datas);
        }
        return $this->SuccessReturn('ok', $datas);
    }

    public function getAllFile($fid = 0)
    {
        $files = FileModel::where('fid', $fid)
            ->where("uid", self::$user['id'])
            ->select();

        if (count($files) > 0) {
            foreach ($files as $v) {
                //获取下级
                $res = FileModel::where('fid', $v['id'])
                    ->where('uid', self::$user['id'])
                    ->find();
                if ($res) {
                    $v['children'] = $this->getAllFile($v['id']);
                }
            }
            return $files;
        } else {
            return $files;
        }
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->post();
        //获取参数
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $fid = array_key_exists('fid', $data) ? $data['fid'] : 0;
        if ($name == '') return $this->ErrorReturn(403, '缺少参数');
        $file_name = FileModel::where('name', $name)->where('fid', $fid)->find();
        if ($file_name) return $this->ErrorReturn(400, '存在相同的文件名称');
        $file = new FileModel();
        $file->name = $name;
        $file->fid = $fid;
        $file->uid = self::$user['id'];
        $res = $file->save();
        if ($res) return $this->SuccessReturn('ok', $file);
        return $this->ErrorReturn(500, '添加失败');
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->put();
        //获取参数
        $id = array_key_exists('id', $data) ? $data['id'] : '';
        $name = array_key_exists('name', $data) ? $data['name'] : '';
        $fid = array_key_exists('fid', $data) ? $data['fid'] : 0;
        if ($id == '') return $this->ErrorReturn(403, '缺少参数');
        $file = FileModel::find($id);
        if (!$file) return $this->ErrorReturn(400, '不存在文件目录');
        $file_sql = FileModel::where('name', $name);
        if ($fid != '') $file_sql->where('fid', $fid);
        $res = $file_sql->find();
        if ($res && $res->id != $id) return $this->ErrorReturn(400, '存在相同的名称');
        $file->name = $name;
        if ($fid != '') $file->fid = $fid;
        $res = $file->save();
        if ($res) return $this->SuccessReturn('ok', $file);
        return $this->ErrorReturn(500, '修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id, Request $request)
    {
        //
        //
        $id = $request->post('id');
        if ($id == 0) return $this->ErrorReturn(400, '数据类型id无效');
        //查询是否存在此文件
        $file = FileModel::where('id', $id)->find();
        if (!$file) return $this->ErrorReturn(404, '找不到此数据类型');
        $data = FileModel::where('fid', $file['id'])->find();
        if ($data) return $this->ErrorReturn(400, '当前目录存在数据 请先处理');
        $file->delete();
        return $this->SuccessReturn('删除成功');
    }
}
